home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.Stanford.EDU!microunity!toms
- From: toms@MicroUnity.com (Tom Sanders)
- Subject: Re: help with pi algorithm
- Message-ID: <DLnHE8.7EK@microunity.com>
- Sender: usenet@microunity.com (news id)
- Organization: MicroUnity Systems Engineering, Inc.
- References: <0fc_9601240111@csource.blaze.net.au>
- Date: Tue, 23 Jan 1996 20:10:55 GMT
-
- In article <0fc_9601240111@csource.blaze.net.au>, Andrew.Nesbit@f396.n634.z3.fidonet.org (Andrew Nesbit) writes:
- |> Hi all!
- |>
- |> I am a newbie C programmer trying to write an algorithm to work out the
- |> value of pi fairly accurately. I have written the following code, but I'm
- |> not sure whether it's considered 'nasty' programming, or whether it could
- |> be written to run more quickly.
- |>
- |> #include <stdio.h>
- |> main()
- |> {
- |> long double pi = 0;
- |> long int = count;
- |> for (count = 1; count <= 300000; count += 4) {
- |> pi += 4.0 / count; pi -= 4.0 / (count + 2);
- |> }
- |> printf("Value of pi is approx %.19Lf)", pi);
- |> return 0;
- |> }
- |>
- |> Any comments please???
- |>
- |> Also, are there any 'standard' algorithm books which would demonstrate this
- |> sort of programming technique?
- |>
- |> Thanks very much,
- |> Andrew Nesbit
- |>
- |> --
- |> Andrew.Nesbit@f396.n634.z3.fidonet.org, Andrew Nesbit 3:634/396 (FidoNet)
- |>
-
- How accurate are you trying to compute pi? Your math library should have
- pi defined to the number of bits available on your platform. On my HPUX
- platform this line is from math.h:
- # define M_PI 3.14159265358979323846
-
- If you want more digits you will have to build an array to hold the
- additional digits.
-
- What algorithm are you trying to implement here?
- For a quick way I'v always liked 355/113 which gives pi to <0.00001%
- error. This algorithm took 2.5 million times through the loop to beet this!
-
- Tom Sanders
-